package com.xjscrm.server.common.util;

import com.alibaba.fastjson.JSONObject;
import com.uzai.mobile.collect.api.dto.msg.MicaFriendTalkMsg;
import com.uzai.mobile.collect.api.dto.trigger.MicaTriggerConversationTalkMsg;
import com.uzai.mobile.collect.api.dto.trigger.talk.*;
import com.uzai.mobile.collect.api.enums.MicaTalkMsgType;
import com.xjscrm.common.utils.TencentCosFileUtil;
import com.xjscrm.common.vo.msg.MsgData;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

/**
 * @author lucher.chen create in 2024/3/1 18:25
 */
@Slf4j
public class MsgDataTools {

    /**
     * 从被动消息获取消息数据到系统消息素材库
     *
     * @param micaFriendTalkMsg 被动消息
     * @author lucher
     * @date 2024/3/6 15:43
     */
    public static MsgData getMsgData(MicaFriendTalkMsg micaFriendTalkMsg) {
        if (micaFriendTalkMsg == null) {
            return null;
        }

        Integer msgType = micaFriendTalkMsg.getMsgType();

        if (msgType == null) {
            return null;
        }
        MsgData msgData = new MsgData();
        msgData.setMsgType(msgType); //消息类型
        if (msgType.intValue() == MicaTalkMsgType.TEXT.getCode().intValue()) {//文本消息
            msgData.setContent(msgData.getContent());
        } else if (msgData.getMsgType().intValue() == MicaTalkMsgType.Picture.getCode().intValue()) {//普通图片消息
            MicaTalkContentUrlMsg urlContent = micaFriendTalkMsg.getUrlContent();
            if (urlContent == null) {
                return null;
            }
            try {
                String newUrl = TencentCosFileUtil.uploadMaterialFileV1(urlContent.getUrl());
                msgData.setImgUrl(newUrl);
            } catch (IOException e) {
                log.error("文件上传失败，url={}",urlContent.getUrl(), e.getLocalizedMessage(), e);
            }
        } else if (msgData.getMsgType().intValue() == MicaTalkMsgType.Voice.getCode().intValue()) {//音频消息
            MicaTalkContentUrlMsg urlContent = micaFriendTalkMsg.getUrlContent();
            if (urlContent == null) {
                return null;
            }
            msgData.setImgUrl(urlContent.getUrl());
        } else if (msgData.getMsgType().intValue() == MicaTalkMsgType.Video.getCode().intValue()) {//视频消息
            MicaTalkContentUrlMsg urlContent = micaFriendTalkMsg.getUrlContent();
            if (urlContent == null) {
                return null;
            }

            //重新上传到腾讯云，下载素材url
            try {
                String url_tencentCos = TencentCosFileUtil.uploadMaterialFileV1(urlContent.getUrl());
                msgData.setVideoUrl(url_tencentCos);
            } catch (Exception e) {
                log.error("文件上传失败，url={}",urlContent.getUrl(), e.getLocalizedMessage(), e);
            }
        } else if (msgData.getMsgType().intValue() == MicaTalkMsgType.Link.getCode().intValue()) { //链接
            MicaTalkContentLinkMsg linkContent = micaFriendTalkMsg.getLinkContent();
            if (linkContent == null) {
                return null;
            }
            msgData.setLinkDesc(linkContent.getDesc());
            msgData.setLinkImageUrl(linkContent.getImageUrl());
            msgData.setLinkTitle(linkContent.getTitle());
            msgData.setLinkUrl(linkContent.getUrl());
        } else if (msgData.getMsgType().intValue() == MicaTalkMsgType.File.getCode().intValue()) {//文件
            MicaTalkContentFileMsg fileContent = micaFriendTalkMsg.getFileContent();
            if (fileContent == null) {
                return null;
            }
            msgData.setFileName(fileContent.getName());
            msgData.setFileUrl(fileContent.getUrl());
        } else if (msgData.getMsgType().intValue() == MicaTalkMsgType.WeApp.getCode().intValue()) {//小程序
            MicaTalkContentMiniprogramMsg miniprogramContent = micaFriendTalkMsg.getMiniprogramContent();
            if (miniprogramContent == null) {
                return null;
            }
            msgData.setMiniProAppId(miniprogramContent.getAppId());
            msgData.setMiniProTitle(miniprogramContent.getTitle());
            msgData.setMiniProDesc(null);
            msgData.setMiniProUrl(miniprogramContent.getPagePath());
            msgData.setMiniImgPwdStr(null);
            msgData.setMiniImgStr(null);
            String mobileImgUrl = miniprogramContent.getMobileImgUrl();
            log.info("mobileImgUrl={}",mobileImgUrl);
            if (StringUtils.isNotBlank(mobileImgUrl)) {
                try {
                    String newUrl = TencentCosFileUtil.uploadMaterialFileV1(mobileImgUrl);
                    log.info("newUrl={}",newUrl);
                    msgData.setMiniImgUrl(newUrl);
                } catch (IOException e) {
                    log.error("文件上传失败，url={}",mobileImgUrl, e.getLocalizedMessage(), e);
                }
            }
            msgData.setMiniProGhId(miniprogramContent.getSourceId());
            msgData.setMiniProName(miniprogramContent.getSourceName());

        } else if (msgData.getMsgType().intValue() == MicaTalkMsgType.Emoji.getCode().intValue()) {//表情
            MicaTalkContentUrlMsg urlContent = micaFriendTalkMsg.getUrlContent();
            if (urlContent == null) {
                return null;
            }
            msgData.setEmojiUrl(urlContent.getUrl());
        } else if (msgData.getMsgType().intValue() == MicaTalkMsgType.ShiPinHao.getCode().intValue()) {//视频号
            MicaTalkContentSphMsg sphContent = micaFriendTalkMsg.getSphContent();
            if (sphContent == null) {
                return null;
            }
            msgData.setSphAuthIcon(sphContent.getAuthIcon());
            msgData.setSphAvatar(sphContent.getAvatar());
            msgData.setSphCover(sphContent.getCover());
            msgData.setSphDes(sphContent.getDes());
            msgData.setSphExtras(sphContent.getExtras());
            msgData.setSphHeight(sphContent.getHeight());
            msgData.setSphId(sphContent.getId());
            msgData.setSphImageCount(sphContent.getImageCount());
            msgData.setSphNickname(sphContent.getNickname());
            msgData.setSphNonceId(sphContent.getNonceId());
            msgData.setSphType(sphContent.getType());
            msgData.setSphWidth(sphContent.getWidth());
            msgData.setSphSize(sphContent.getSize());
            msgData.setSphThumbSize(sphContent.getThumbSize());
            msgData.setSphThumbUrl(sphContent.getThumbUrl());
            msgData.setSphUrl(sphContent.getUrl());
        }

        return msgData;
    }

    /**
     * 从后台消息数据转化成手机端消息内容字符串
     *
     * @param msgData 后台消息类型
     * @author lucher
     * @date 2024/3/6 15:43
     */
    public static String getTalkMsgContent(MsgData msgData) {
        if (msgData == null) {
            return null;
        }

        Integer msgType = msgData.getMsgType();
        if (msgType == null) {
            return null;
        }
        String talkMsgContent = null;

        MicaTriggerConversationTalkMsg micaTriggerConversationTalkMsg = new MicaTriggerConversationTalkMsg();
        if (msgData.getMsgType().intValue() == MicaTalkMsgType.TEXT.getCode().intValue()) {//文本消息
            MicaTalkContentTextMsg micaTalkContentTextMsg = new MicaTalkContentTextMsg();
            micaTalkContentTextMsg.setText(msgData.getContent());
            talkMsgContent = JSONObject.toJSONString(micaTalkContentTextMsg);
        } else if (msgData.getMsgType().intValue() == MicaTalkMsgType.Picture.getCode().intValue()) {//普通图片消息
            MicaTalkContentUrlMsg micaTalkContentUrlMsg = new MicaTalkContentUrlMsg();
            micaTalkContentUrlMsg.setUrl(msgData.getImgUrl());
            talkMsgContent = JSONObject.toJSONString(micaTalkContentUrlMsg);
        } else if (msgData.getMsgType().intValue() == MicaTalkMsgType.Voice.getCode().intValue()) {//音频消息
            MicaTalkContentUrlMsg micaTalkContentUrlMsg = new MicaTalkContentUrlMsg();
            micaTalkContentUrlMsg.setUrl(msgData.getVoicesUrl());
            talkMsgContent = JSONObject.toJSONString(micaTalkContentUrlMsg);
        } else if (msgData.getMsgType().intValue() == MicaTalkMsgType.Video.getCode().intValue()) {//视频消息
            MicaTalkContentUrlMsg micaTalkContentUrlMsg = new MicaTalkContentUrlMsg();
            micaTalkContentUrlMsg.setUrl(msgData.getVideoUrl());
            talkMsgContent = JSONObject.toJSONString(micaTalkContentUrlMsg);
        } else if (msgData.getMsgType().intValue() == MicaTalkMsgType.Link.getCode().intValue()) { //链接
            MicaTalkContentLinkMsg micaTalkContentLinkMsg = new MicaTalkContentLinkMsg();
            micaTalkContentLinkMsg.setDesc(msgData.getLinkDesc());
            micaTalkContentLinkMsg.setTitle(msgData.getLinkTitle());
            micaTalkContentLinkMsg.setImageUrl(msgData.getLinkImageUrl());
            micaTalkContentLinkMsg.setUrl(msgData.getLinkUrl());
            talkMsgContent = JSONObject.toJSONString(micaTalkContentLinkMsg);
        } else if (msgData.getMsgType().intValue() == MicaTalkMsgType.File.getCode().intValue()) {//文件
            MicaTalkContentFileMsg micaTalkContentFileMsg = new MicaTalkContentFileMsg();
            micaTalkContentFileMsg.setUrl(msgData.getFileUrl());
            micaTalkContentFileMsg.setName(msgData.getFileName());
            talkMsgContent = JSONObject.toJSONString(micaTalkContentFileMsg);
        } else if (msgData.getMsgType().intValue() == MicaTalkMsgType.WeApp.getCode().intValue()) {//小程序
            MicaTalkContentMiniprogramMsg micaTalkContentMiniprogramMsg = new MicaTalkContentMiniprogramMsg();
            micaTalkContentMiniprogramMsg.setAppId(msgData.getMiniProAppId());
            micaTalkContentMiniprogramMsg.setMobileImgUrl(msgData.getMiniImgUrl());
            micaTalkContentMiniprogramMsg.setPagePath(msgData.getMiniProUrl());
            micaTalkContentMiniprogramMsg.setTitle(msgData.getMiniProTitle());
            micaTalkContentMiniprogramMsg.setSourceId(msgData.getMiniProGhId());
            micaTalkContentMiniprogramMsg.setSourceName(msgData.getMiniProName());
            talkMsgContent = JSONObject.toJSONString(micaTalkContentMiniprogramMsg);
        } else if (msgData.getMsgType().intValue() == MicaTalkMsgType.Emoji.getCode().intValue()) {//表情
            MicaTalkContentUrlMsg micaTalkContentUrlMsg = new MicaTalkContentUrlMsg();
            micaTalkContentUrlMsg.setUrl(msgData.getEmojiUrl());
            talkMsgContent = JSONObject.toJSONString(micaTalkContentUrlMsg);
        } else if (msgData.getMsgType().intValue() == MicaTalkMsgType.ShiPinHao.getCode().intValue()) {//视频号
            MicaTalkContentSphMsg micaTalkContentSphMsg = new MicaTalkContentSphMsg();
            micaTalkContentSphMsg.setAuthIcon(msgData.getSphAuthIcon());
            micaTalkContentSphMsg.setAvatar(msgData.getSphAvatar());
            micaTalkContentSphMsg.setCover(msgData.getSphCover());
            micaTalkContentSphMsg.setDes(msgData.getSphDes());
            micaTalkContentSphMsg.setExtras(msgData.getSphExtras());
            micaTalkContentSphMsg.setHeight(msgData.getSphHeight());
            micaTalkContentSphMsg.setId(msgData.getSphId());
            micaTalkContentSphMsg.setImageCount(msgData.getSphImageCount());
            micaTalkContentSphMsg.setNickname(msgData.getSphNickname());
            micaTalkContentSphMsg.setNonceId(msgData.getSphNonceId());
            micaTalkContentSphMsg.setType(msgData.getSphType());
            micaTalkContentSphMsg.setWidth(msgData.getSphWidth());
            micaTalkContentSphMsg.setSize(msgData.getSphSize());
            micaTalkContentSphMsg.setThumbSize(msgData.getSphThumbSize());
            micaTalkContentSphMsg.setThumbUrl(msgData.getSphThumbUrl());
            micaTalkContentSphMsg.setUrl(msgData.getSphUrl());
            talkMsgContent = JSONObject.toJSONString(micaTalkContentSphMsg);
        }
        return talkMsgContent;
    }


}
